package com.shouer.doc.ui.modifypass;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.shouer.doc.application.App;
import com.shouer.doc.R;
import com.shouer.doc.common.util.SPUtils;
import com.shouer.doc.dialog.ShouerLoadingDialogUtil;
import com.shouer.doc.event.RemoteLoginEvent;
import com.shouer.doc.event.RongRemoteLoginEvent;
import com.shouer.doc.ui.login.LoginActivity;
import com.shouer.doc.ui.modifypass.presenter.IModifyPassPresenter;
import com.shouer.doc.ui.modifypass.presenter.ModifyPassPresenterCompl;
import com.shouer.doc.ui.modifypass.view.IModifyPassView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.OnTextChanged;

/**
 * Created by ZXL on 2016/12/5.
 */

public class ModifyPassActivity extends AppCompatActivity implements IModifyPassView {

    @BindView(R.id.old_pass)
    protected EditText oldPassInput;
    @BindView(R.id.new_pass1)
    protected EditText newPassInput1;
    @BindView(R.id.new_pass2)
    protected EditText newPassInput2;
    @BindView(R.id.old_del)
    protected View oldDelView;
    @BindView(R.id.new_del1)
    protected View newDelView1;
    @BindView(R.id.new_del2)
    protected View newDelView2;
    @BindView(R.id.ok_btn)
    protected View modifyBtn;
    @BindView(R.id.back_btn)
    protected View backBtn;
    private IModifyPassPresenter presenter;

    private App app;
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_modify_pass_layout);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        app= (App) getApplicationContext();
        presenter=new ModifyPassPresenterCompl(this);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        presenter.onDestroy();
    }
    @OnClick(value = {R.id.back_btn, R.id.ok_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.ok_btn:
                modifyBtn.setClickable(false);
                String oldPassString=oldPassInput.getText().toString();
                if (oldPassString.length()<6)
                {
                    Toast.makeText(this,"原密码长度至少6位",Toast.LENGTH_SHORT).show();
                    modifyBtn.setClickable(true);
                    return;
                }
                String newPassString1=newPassInput1.getText().toString();
                if (newPassString1.length()<6)
                {
                    Toast.makeText(this,"新密码长度至少6位",Toast.LENGTH_SHORT).show();
                    modifyBtn.setClickable(true);
                    return;
                }
                String newPassString2=newPassInput2.getText().toString();
                if (newPassString1.equals(newPassString2))
                {
                    backBtn.setClickable(false);
                    ShouerLoadingDialogUtil.getInstance().show(this);
                    presenter.modifyPassword(oldPassString,newPassString1);
                }else
                {
                    Toast.makeText(this,"两次输入新密码不一样 请重新输入",Toast.LENGTH_SHORT).show();
                    modifyBtn.setClickable(true);
                    return;
                }
                break;
        }
    }


    @OnFocusChange(value = {R.id.old_pass, R.id.new_pass1, R.id.new_pass2})
    public void onFocusChange(View view, boolean hasFocus) {
        switch (view.getId()) {
            case R.id.old_pass:
                if (hasFocus && oldPassInput.getText().length() > 0) {
                    oldDelView.setVisibility(View.VISIBLE);
                } else {
                    oldDelView.setVisibility(View.GONE);
                }
                break;
            case R.id.new_pass1:
                if (hasFocus && newPassInput1.getText().length() > 0) {
                    newDelView1.setVisibility(View.VISIBLE);
                } else {
                    newDelView1.setVisibility(View.GONE);
                }
                break;
            case R.id.new_pass2:
                if (hasFocus && newPassInput2.getText().length() > 0) {
                    newDelView2.setVisibility(View.VISIBLE);
                } else {
                    newDelView2.setVisibility(View.GONE);
                }
                break;
        }
    }



    @OnTextChanged(value = {R.id.old_pass},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged1(Editable s)
    {
        if (oldPassInput.hasFocus())
        {
            if (s.length()>0)
            {
                oldDelView.setVisibility(View.VISIBLE);
            }else
            {
                oldDelView.setVisibility(View.GONE);
            }
        }
    }

    @OnTextChanged(value = {R.id.new_pass1},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged2(Editable s)
    {
        if (newPassInput1.hasFocus())
        {
            if (s.length()>0)
            {
                newDelView1.setVisibility(View.VISIBLE);
            }else
            {
                newDelView1.setVisibility(View.GONE);
            }
        }
    }

    @OnTextChanged(value = {R.id.new_pass2},callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    public void afterTextChanged3(Editable s)
    {
        if (newPassInput2.hasFocus())
        {
            if (s.length()>0)
            {
                newDelView2.setVisibility(View.VISIBLE);
            }else
            {
                newDelView2.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public App getApp() {
        return app;
    }

    @Override
    public void modifyPassSuccess() {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        Toast.makeText(getContext(),"密码修改成功",Toast.LENGTH_SHORT).show();
        SPUtils.put(ModifyPassActivity.this,"user_phone","");
        getApp().loginOut();
        Intent loginIntent=new Intent(ModifyPassActivity.this, LoginActivity.class);
        startActivity(loginIntent);
        finish();
    }

    @Override
    public void modifyPassFailed(String failInfo) {
        ShouerLoadingDialogUtil.getInstance().dismiss();
        modifyBtn.setClickable(true);
        backBtn.setClickable(true);
        Toast.makeText(this,failInfo,Toast.LENGTH_SHORT).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RemoteLoginEvent event)
    {
        finish();
    }

    /**
     * 账号异地登录 关闭Activity
     * */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(RongRemoteLoginEvent event) {
        finish();
    }
}
